//
//  HQCollectViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/6/19.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQCollectViewController.h"

@interface HQCollectViewController ()

@end

@implementation HQCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的收藏";
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemTitle:@"搜索" withTitleColor:[self colorHexValue:0x647180] withFont:16 withTarget:self withSelector:@selector(searchItem)];
    _dataSource = [[NSMutableArray alloc]init];
    
    // 下拉刷新
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pageNo = 1;
        [self requestShopData];
    }];
    [_tableView.mj_header beginRefreshing];
}

// 上拉刷新
-(void)addFooter{
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _pageNo++;
        [self requestShopData];
    }];
}

/**
 搜索按钮
 */
-(void)searchItem{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"输入搜索关键词" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
    UITextField *txtName = [alert textFieldAtIndex:0];
    txtName.placeholder = @"请输入搜索关键词";
    txtName.text = _currentTextField.text;
    alert.tag = 1001;
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(alertView.tag == 1001){
        if (buttonIndex == 1) {
            _currentTextField = [alertView textFieldAtIndex:0];
            [self requestShopData];
        }
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HQCollectViewCell *cell = [HQCollectViewCell cellWithTableView:tableView];
    cell.dataInfo = _dataSource[indexPath.row];
    [cell.lookBigItem addTarget:self action:@selector(lookBigItem:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    
    HQNewProductDetailViewController *productDetailVc = [[HQNewProductDetailViewController alloc]init];
    productDetailVc.dataInfo = _dataSource[indexPath.row];
    productDetailVc.pushType = @"我的收藏";
    productDetailVc.productListArray = _dataSource;
    productDetailVc.currentIndex = indexPath.row;
    [self.navigationController pushViewController:productDetailVc animated:YES];
}

/************************************************************************************************************************************/
/************************************************************************************************************************************/
/*******************************************************搜索条件相关方法****************************************************************/
/************************************************************************************************************************************/
/************************************************************************************************************************************/
/**
 请求店内
 */
-(void)requestShopData{
    _tableView.backgroundView = nil;
    [self showHudText:@""];
    id thz_id = _dict1?_dict1[@"shop_name"]:nil;
    id tse_id = _dict2?_dict2[@"tse_id"]:nil;
    id price_dt = _dict3[@"tpe_begpri"]?[NSString stringWithFormat:@"%@-%@",_dict3[@"tpe_begpri"],_dict3[@"tpe_endpri"]]:nil;
    id sortprice = _dict4?_dict4[@"sort"]:nil;
    
    if(_dict1||_dict2||_dict3||_dict4||_dict5){
        [_sxItem setTitleColor:[self colorHexValue:0x007AFF] forState:UIControlStateNormal];
    }else{
        [_sxItem setTitleColor:[self colorHexValue:0x333333] forState:UIControlStateNormal];
    }
    
    if(_dict1){
        [_cjxzItem setTitleColor:[self colorHexValue:0x007AFF] forState:UIControlStateNormal];
    }else{
        [_cjxzItem setTitleColor:[self colorHexValue:0x333333] forState:UIControlStateNormal];
    }
    
    if(_dict2){
        [_cpfgItem setTitleColor:[self colorHexValue:0x007AFF] forState:UIControlStateNormal];
    }else{
        [_cpfgItem setTitleColor:[self colorHexValue:0x333333] forState:UIControlStateNormal];
    }
    
    if(_dict3){
        [_jgqjItem setTitleColor:[self colorHexValue:0x007AFF] forState:UIControlStateNormal];
    }else{
        [_jgqjItem setTitleColor:[self colorHexValue:0x333333] forState:UIControlStateNormal];
    }
    
    // 获取收藏数据
    [HQNetWork requetParam:[HQParam collectionListcoll:[UserModelManager userID] cs_name:thz_id tse_id:tse_id price_dt:price_dt sortprice:sortprice searchtitle:_currentTextField.text page:[NSString stringWithFormat:@"%ld",_pageNo] pagenum:@"10"] withMethod:[HQMethod collectionListcoll] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        [_tableView.mj_footer endRefreshing];
        [_tableView.mj_header endRefreshing];
        if([sucData[kStatus]integerValue] == 1){
            NSArray *tempArr = sucData[@"data"];
            if(_pageNo == 1){
                [_dataSource removeAllObjects];
                [_dataSource addObjectsFromArray:tempArr];
                if(_dataSource.count ==0){
                    [self addNoneDataView:_tableView withText:@"列表内容为空"];
                }
                if(_dataSource.count >=10){
                    [self addFooter];
                }else{
                    [_tableView.mj_footer removeFromSuperview];
                }
            }else{
                [_dataSource addObjectsFromArray:tempArr];
            }
            [_tableView reloadData];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
        [_tableView.mj_footer endRefreshing];
        [_tableView.mj_header endRefreshing];
    }];
}

/**
 厂家选择
 
 @param sender 按钮
 */
- (IBAction)changJiaXuanZeItem:(id)sender {
    [self hidden2];
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam collectionGetsearchdata:[UserModelManager userID]] withMethod:[HQMethod collectionGetsearchdata] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus] integerValue]== 1){
            NSDictionary *dict = sucData[@"data"];
            
            _view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, self.view.frame.size.height - 44)];
            _view2.backgroundColor = [UIColor blackColor];
            _view2.alpha = 0.3;
            [self.view addSubview:_view2];
            [self.view bringSubviewToFront:_view2];
            UITapGestureRecognizer *g = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hidden2)];
            [_view2 addGestureRecognizer:g];
            
            _v2 = [HQShopFiltrateConditionView loadShopFiltrateView];
            NSArray *fctlistsArr = dict[@"fctlists"] != nil?dict[@"fctlists"]:@[@{@"shop_name":@"全部"}];
            [_v2 selectDict:_dict1];
            _v2.type = @"fctlists";
            _v2.delegate = self;
            _v2.dataSource = [[NSMutableArray alloc]initWithArray:fctlistsArr];
            _v2.frame = CGRectMake(0, 44, SCREEN_WIDTH, 260);
            [self.view addSubview:_v2];
            [self.view bringSubviewToFront:_v2];
            
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 产品风格
 
 @param sender 按钮
 */
- (IBAction)chanPinFengGeItem:(id)sender {
    [self hidden2];
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam collectionGetsearchdata:[UserModelManager userID]] withMethod:[HQMethod collectionGetsearchdata] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus] integerValue]== 1){
            NSDictionary *dict = sucData[@"data"];
            
            _view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, self.view.frame.size.height - 44)];
            _view2.backgroundColor = [UIColor blackColor];
            _view2.alpha = 0.3;
            [self.view addSubview:_view2];
            [self.view bringSubviewToFront:_view2];
            UITapGestureRecognizer *g = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hidden2)];
            [_view2 addGestureRecognizer:g];
            
            _v2 = [HQShopFiltrateConditionView loadShopFiltrateView];
            NSArray *a = dict[@"stylelists"];
            _v2.type = @"stylelists";
            [_v2 selectDict:_dict2];
            _v2.delegate = self;
            _v2.dataSource = [[NSMutableArray alloc]initWithArray:a];
            _v2.frame = CGRectMake(0, 44, SCREEN_WIDTH, 260);
            [self.view addSubview:_v2];
            [self.view bringSubviewToFront:_v2];
            
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 价格区间
 
 @param sender 按钮
 */
- (IBAction)jiaGeQuJianItem:(id)sender {
    [self hidden2];
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam collectionGetsearchdata:[UserModelManager userID]] withMethod:[HQMethod collectionGetsearchdata] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus] integerValue]== 1){
            NSDictionary *dict = sucData[@"data"];
            
            _view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, self.view.frame.size.height - 44)];
            _view2.backgroundColor = [UIColor blackColor];
            _view2.alpha = 0.3;
            [self.view addSubview:_view2];
            [self.view bringSubviewToFront:_view2];
            UITapGestureRecognizer *g = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hidden2)];
            [_view2 addGestureRecognizer:g];
            
            _v2 = [HQShopFiltrateConditionView loadShopFiltrateView];
            NSArray *a = dict[@"pricelists"];
            [_v2 selectDict:_dict3];
            _v2.delegate = self;
            _v2.type = @"pricelists";
            _v2.dataSource = [[NSMutableArray alloc]initWithArray:a];
            _v2.frame = CGRectMake(0, 44, SCREEN_WIDTH, 260);
            [self.view addSubview:_v2];
            [self.view bringSubviewToFront:_v2];
            
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 筛选
 
 @param sender 按钮
 */
- (IBAction)shaiXuanItem:(id)sender {
    [self hidden2];
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam collectionGetsearchdata:[UserModelManager userID]] withMethod:[HQMethod collectionGetsearchdata] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus] integerValue]== 1){
            NSLog(@"搜索条件返回数据是: %@",sucData);
            
            NSDictionary *dict = sucData[@"data"];
            
            UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
            _view2 = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
            _view2.backgroundColor = [UIColor blackColor];
            _view2.alpha = 0.3;
            [keyWindow addSubview:_view2];
            
            UITapGestureRecognizer *g = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hidden2)];
            [_view2 addGestureRecognizer:g];
            
            _b = [[UIView alloc]init];
            _b.frame = CGRectMake(50, 0, SCREEN_WIDTH-50, SCREEN_HEIGHT);
            
            HQShopFiltrateView *v = [HQShopFiltrateView loadShopFiltrateView];
            v.delegate = self;
            
            NSArray *fctlistsArr = dict[@"fctlists"] != nil?dict[@"fctlists"]:@[@{@"shop_name":@"全部"}];
            NSArray *a = @[
                           @{@"itemName":@"厂家选择",@"itemArray":fctlistsArr},
                           @{@"itemName":@"产品风格",@"itemArray":dict[@"stylelists"]},
                           @{@"itemName":@"价格区间",@"itemArray":dict[@"pricelists"]},
                           @{@"itemName":@"价格排序",@"itemArray":dict[@"sortprice"]}];
            v.dataSource = [[NSMutableArray alloc]initWithArray:a];
            [v selctDict:_dict1 withDict2:_dict2 withDict3:_dict3 withDict4:_dict4 withDict5:_dict5];
            v.frame = _b.bounds;
            [_b addSubview:v];
            _b.backgroundColor = [UIColor whiteColor];
            [keyWindow addSubview:_b];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 删除筛选视图
 */
-(void)hidden2{
    [_view2 removeFromSuperview];
    [_b removeFromSuperview];
    [_v2 removeFromSuperview];
}

#pragma 厂家选择-产品风格-价格区间
-(void)filterDict:(NSDictionary *)dict withType:(NSString *)type{
    NSLog(@"%@=%@",dict,type);
    [self hidden2];
    if([type isEqualToString:@"fctlists"]){
        // 厂家
        _dict1 = dict;
    }else if([type isEqualToString:@"stylelists"]){
        // 产品风格
        _dict2 = dict;
    }else if([type isEqualToString:@"pricelists"]){
        // 价格区间
        _dict3 = dict;
    }
    [self requestShopData];
}

#pragma 筛选代理方法
-(void)filter2Dict:(NSDictionary *)dict withDict2:(NSDictionary *)dict2 withDict3:(NSDictionary *)dict3 withDict4:(NSDictionary *)dict4 withDict5:(NSDictionary *)dict5{
    NSLog(@"%@=%@=%@=%@=%@",dict,dict2,dict3,dict4,dict5);
    _dict1 = dict;
    _dict2 = dict2;
    _dict3 = dict3;
    _dict4 = dict4;
    _dict5 = dict5;
    [self hidden2];
    [self requestShopData];
}
/************************************************************************************************************************************/
/************************************************************************************************************************************/
/************************************************************************************************************************************/
/************************************************************************************************************************************/

/**
 查看大图
 
 @param sender 按钮
 */
-(void)lookBigItem:(HQButton *)sender{
    HQBigImageViewController *bigImageVc = [[HQBigImageViewController alloc]init];
    bigImageVc.imageURL = sender.dataInfo[@"s_img"];
    [self presentViewController:bigImageVc animated:YES completion:nil];
}

@end
